package com.xr.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xr.constant.MessageConstant;
import com.xr.exception.CustomException;
import com.xr.pojo.CheckGroup;
import com.xr.query.QueryPageBean;
import com.xr.result.PageResult;
import com.xr.result.Result;
import com.xr.service.CheckGroupService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("checkGroup")
public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;


    //查询所有
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<CheckGroup> all = checkGroupService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,all);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_FAIL);
    }


    //条件查询加分页
    @RequestMapping("/findItemsByGroupId")
    public Result findItemsByGroupId(Integer id){
        System.out.println("yyyyy:"+id);
        try {
            List<Integer> Ids = checkGroupService.selectItemsByGroupId(id);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,Ids);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_FAIL);
    }

    //修改

    @RequestMapping("/UpdCheckGroup")
    public Result UpdCheckGroup(@RequestBody CheckGroup checkGroup,Integer [] checkitemIds){
        System.out.println("join Upd");
        try {
            checkGroupService.UpdCheckGroup(checkGroup,checkitemIds);
            return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
    }


    //添加
    @RequestMapping("/addCheckGroup")
    public Result addCheckGroup(@RequestBody CheckGroup checkGroup,Integer [] checkitemIds){
        System.out.println("join add");
        try {
            checkGroupService.AddCheckGroup(checkGroup,checkitemIds);
            return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
    }

    //条件查询加分页
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        System.out.println("分页条件:"+queryPageBean);
        try {
            PageResult pageResult = checkGroupService.findPage(queryPageBean);
            System.out.println("分页结果："+pageResult.getRows());
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,pageResult);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_FAIL);
    }

    //删除
    @RequestMapping("/DelCheckGroup")
    public Result DelCheckGroup(Integer id) throws CustomException {
        Integer row = checkGroupService.DelCheckGroup(id);
        return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS,row);
    }
}
